package io.lckgroup.modules.lck.controller.app;

import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.R;
import io.lckgroup.modules.lck.annotation.AppLogin;
import io.lckgroup.modules.lck.annotation.AppLoginUser;
import io.lckgroup.modules.lck.entity.OrderMainEntity;
import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.entity.appvo.OrderMainEntityVo;
import io.lckgroup.modules.lck.service.OrderDetailService;
import io.lckgroup.modules.lck.service.OrderMainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP Do订单主表
 *
 * @author srq
 * @email siruiqi@siruiqi.com
 * @date 2020-2-6 16:21:51
 */
@RestController
@RequestMapping("/app/orderMain")
@Api("APP订单主表")
public class AppOrderMainController {

    @Autowired
    private OrderMainService orderMainService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 获取已支付订单的列表
     *
     * @param params
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("getPayList")
    @ApiOperation("获取已支付订单的列表")
    public R getPayList(@RequestParam Map<String, Object> params, @AppLoginUser ShopMerchantInfoEntity sm) {
        //获取商户当前登录用户的商户信息
        ShopMerchantInfoEntity merchant = sm;
        OrderMainEntity om = new OrderMainEntity();
        om.setMerchantId(merchant.getId());
        om.setIsPay(1);//已支付
        //需要传入一个page和limit,还需要封装查询条件
        PageUtils queryPage = orderMainService.queryPageByEntity(params, om);
        return R.ok().put("results", queryPage).put("msg", "成功");

    }

    /**
     * 获取已结单的订单的列表
     *
     * @param params
     * @param sm
     * @return R
     */
    @AppLogin
    @GetMapping("getFinishList")
    @ApiOperation("获取已结单的订单的列表")
    public R getFinishList(@RequestParam Map<String, Object> params, @AppLoginUser ShopMerchantInfoEntity sm) {
        //获取商户当前登录用户的商户信息
        ShopMerchantInfoEntity merchant = sm;
        OrderMainEntity om = new OrderMainEntity();
        om.setMerchantId(merchant.getId());
        om.setIsPay(3);//已结单
        //需要传入一个page和limit,还需要封装查询条件
        PageUtils queryPage = orderMainService.queryPageByEntity(params, om);
        return R.ok().put("results", queryPage).put("msg", "成功");

    }

    /**
     * 通过主键修改已支付订单为已结单
     *
     * @param id
     * @return R
     */
    @AppLogin
    @GetMapping("/dealOrderById/{id}")
    @ApiOperation("通过主键修改已支付订单为已结单")
    @CacheEvict(value = "weixinGetOrderList", key = "#sm.id")
    public R dealOrderById(@PathVariable("id") String id, @AppLoginUser ShopMerchantInfoEntity sm) {
        OrderMainEntity om = new OrderMainEntity();
        om.setId(Long.parseLong(id));
        om.setIsPay(3);
        boolean result = orderMainService.updateById(om);
        if (result) {
            return R.ok().put("msg", "操作成功");
        } else {
            return R.error(-1, "操作失败");
        }

    }

    /**
     * 通过主键获取订单信息
     *
     * @param id
     * @return R
     */
    @AppLogin
    @GetMapping("/getOrderMsgById/{id}")
    @ApiOperation("通过主键获取订单信息")
    public R getOrderMsgById(@PathVariable("id") String id) {
        //获取订单信息
        OrderMainEntityVo om = orderMainService.getMsgDefineById(id);
        //获取订单明细信息
        List<Map<String, Object>> list = orderDetailService.getOrderDetailByOrderId(Integer.valueOf(id));
        Map<String, Object> map = new HashMap<>();
        return R.ok().put("results", om).put("list", list);
    }

}
